package com.xc.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.pojo.Dept;
import com.xc.service.DeptService;
import com.xc.service.SysUserService;
import com.xc.utils.DataGridViewResult;
import com.xc.utils.SysConstants;
import com.xc.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private SysUserService sysUserService;
    /**
     *  查询部门列表
     * @param deptVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(DeptVo deptVo){
        //设置分页信息(当前页码,每页显示数量)
        PageHelper.startPage(deptVo.getPage(),deptVo.getLimit());
        //调用分页查询列表的方法
        List<Dept> deptList = deptService.queryDeptList(deptVo);
        //创建分页对象
        PageInfo<Dept> pageInfo=new PageInfo<Dept>(deptList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @RequestMapping("/addDept")
    public String addDept(Dept dept){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (deptService.addDept(dept)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"添加成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"添加失败");
        }
      return JSON.toJSONString(map);
    }

    /**
     * 修改部门信息
     * @param dept
     * @return
     */
    @RequestMapping("/updateDept")
    public String updateDept(Dept dept){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (deptService.updateDept(dept)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"添加成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询部门是否存在用户
     * @return
     */
    @RequestMapping("/checkDeptUser")
    public String checkDeptUser(Integer deptId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (sysUserService.getUserByDeptId(deptId)>0){
            map.put(SysConstants.EXIST,true);
            map.put(SysConstants.MESSAGE,"该部门存在用户，无法删除");
        }else {
            map.put(SysConstants.EXIST,false);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping("/deleteDept")
    public String deleteDept(Integer id){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (deptService.deleteDept(id)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"删除成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/queryDeptList")
    public String queryDeptList(){
        //为了避免重复查询数据库 现在用redis缓存进行优化处理
        return deptService.queryDeptList();
    }
}
